package Main;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.swing.JOptionPane;

import org.omg.CORBA._IDLTypeStub;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;


public class Level {
	Map m_Map;
	MyCube m_Cube;
	ManColitionAndTest m_Man;
	int LevelID=1;
	public Boolean m_testStart=false;
	public ItemLevel _ItemLevel;
	public Boolean m_TestCom=false;
	public Boolean m_TestOver=false;
	Texture m_Back;
	public Level()
	{
		m_Man=new ManColitionAndTest();
		_ItemLevel=new ItemLevel();
	}
	public Level(int _levelID)
	{
		LevelID=_levelID;
		m_Man=new ManColitionAndTest();
		_ItemLevel=new ItemLevel();
	}
	public void Init(int _levelID)
	{
		LevelID=_levelID;
		m_testStart=false;
		m_TestCom=false;
		m_TestOver=false;
		m_Map=new Map(LevelID);
		m_Cube=new MyCube(m_Map.PosXCube,m_Map.PosYCube);
		m_Cube.m_Cube_TestFlyDown=true;
		m_Cube.m_Cube_Z=5;
		_ItemLevel.Init(LevelID);
		m_Back=LoadTexture("back"+LevelID+".png");
		m_Back.enable();
	}
	public void Init()
	{
		m_testStart=false;
		m_TestCom=false;
		m_TestOver=false;
		m_Map=new Map(LevelID);
		m_Cube=new MyCube(m_Map.PosXCube,m_Map.PosYCube);
		m_Cube.m_Cube_TestFlyDown=true;
		m_Cube.m_Cube_Z=5;
		_ItemLevel.Init(LevelID);
		m_Back=LoadTexture("back"+LevelID+".png");
		m_Back.enable();
	}
	public Texture LoadTexture(String _path){
		Texture _result;
		BufferedImage image;
		try{
			image = ImageIO.read(this.getClass().getResource(_path));
			_result = TextureIO.newTexture(image,false);
		}catch(IllegalArgumentException e){
			JOptionPane.showMessageDialog(null, "Can not load file \"" + _path + "\". The application will be exit !"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			//System.exit(0);
			return null;
		}catch(IOException e){
			e.printStackTrace();
			return null;
		}
		return _result;
		
	}
	public void Display(GL gl)
	{
		
		if(m_testStart==true)
		{
			if(m_TestCom==false)
			{
				m_Cube.FlyDown();
				if(m_Cube.m_Cube_Z<=0)
				{
					m_Cube.m_Cube_TestFlyDown=false;
					m_testStart=true;
					m_TestCom=true;
					m_Cube.m_Cube_RoZ=0;
					m_Cube.m_Cube_Z=0;
				}
			}
			update();
			m_Map.DrawMap(gl);
			m_Cube.Display(gl,m_Map);
			gl.glPushMatrix();
			m_Back.bind();
			gl.glBegin(GL.GL_QUADS);
			
	        // Front Face
	        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( -15, 0, -5);
	        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 35, 0, -5);
	        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 35, 35, -5);
	        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( -15, 35, -5);
	        gl.glEnd();
	        gl.glPopMatrix();
		}
		else
		{
			_ItemLevel.Display(gl, this);
		}
	}
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
			m_Cube.KeyPressed(arg0);

	}
	
	public void update()
	{
		
		m_Cube.UpdateRotation();
		
		m_Man.TestColition(m_Map,m_Cube);
		if(m_Cube.m_Cube_TestRef==true)
		{
			Init();
		}
		if(m_Map.m_TestEnd==true )
		{
			m_Cube.Fly();
			if(m_Cube.m_Cube_Z>=10)
				{
					LevelID+=1;
					if(LevelID<=7)
						if(LevelID>SaveFile.m_FinalLevel)
						{
							SaveFile.m_FinalLevel=LevelID;
							try {
								SaveFile.WriteFile(DefineCons.SAVE_FILE);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					if(LevelID>=8)
					{
						m_TestOver=true;
						return;
					}
					Init();
					m_Cube.m_Cube_TestFlyUp=false;
				}	
		}
	}
	
}
